package com.itchen.school.common.utils;

import com.itchen.school.common.exception.SysException;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.util.Base64;

@Slf4j
public class FileUtils extends SysFileUtils {

    /**
     * 文件转base64字符串
     * @param file 文件
     * @return base64字符串
     */
    public static String fileToBase64(File file) {
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] buffer = new byte[(int) file.length()];
            fis.read(buffer);
            return Base64.getEncoder().encodeToString(buffer);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new SysException("文件转换Base64失败！");
        }
    }

}

